package com.yu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yu.bean.Employee;
import com.yu.bean.Salary;
import com.yu.service.EmployeeService;
import com.yu.service.SalaryService;

@Controller
@RequestMapping("/salary")
public class SalaryController {
	
	@Autowired
	private SalaryService salaryService;

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping("/getAll")
	public String getAll(Model model) {
		List<Salary> list = salaryService.getAllWithEmployee();
		model.addAttribute("list", list);
		return "salary/showList";
	}

	@RequestMapping("/toAdd")
	public String toAdd(Model model) {
		List<Employee> list = employeeService.getAll();
		model.addAttribute("employeeList", list);
		return "salary/add";
	}

	@RequestMapping("/add")
	public String add(Salary salary) {
		salaryService.add(salary);
		return "forward:/salary/getAll.do";
	}

	@RequestMapping("/delete")
	public String delete(int id) {
		salaryService.delete(id);
		return "forward:/salary/getAll.do";
	}

	@RequestMapping("/toEdit")
	public String toEdit(Model model, int id) {
		Salary salary = salaryService.getById(id);
		model.addAttribute("salary", salary);
		List<Employee> list = employeeService.getAll();
		model.addAttribute("employeeList", list);
		return "salary/edit";
	}

	@RequestMapping("/edit")
	public String edit(Salary salary) {
		salaryService.update(salary);
		return "forward:/salary/getAll.do";
	}
}
